

public class exceptionDemo {

    public static void main(String[] args) {
        Bar bar = new Bar();
        try{
            bar.enter(15);
        }catch(AgeLessThanEighteenException e){
            System.out.println("错误信息："+e.getMessage());
        }
    }

}
//定义了一个异常类
class AgeLessThanEighteenException extends Exception{
    private String message;//描述异常信息
    public AgeLessThanEighteenException(String message){
        this.message = message;
    }
    public String getMessage(){
        return message;
    }
}
class Bar{
    public void enter(int age)throws AgeLessThanEighteenException{
        if(age<18){
            throw new AgeLessThanEighteenException("未成年");
        }
        else{
            System.out.println("欢迎光临");
        }
    }
}